import type { MutationTree, ActionTree, GetterTree } from 'vuex'

// import { Message } from 'element-ui'
import type { IRootState } from './index'
import { Actions, Mutations } from '@/enum/user'
import { login, logout, phoneLogin } from '@/api/login'

import type { ILoginParams, IPhoneLoginParams } from '@/api/login'
import { getCurrentUser } from '@/api/user'
import { setToken } from '@/utils/token'
import type { IMenu } from '@/api/menu'
import { findCurrentUserMenu } from '@/api/menu'

function setMenuIcon(menu: IMenu): IMenu {
  switch (menu.path) {
    case '/admin/card': {
      menu.icon = 'el-icon-setting'
      break
    }
    case '/admin/manage': {
      menu.icon = 'el-icon-tickets'
      break
    }
    case '/admin/system': {
      menu.icon = 'el-icon-user'
      break
    }
    case '/admin/menus': {
      menu.icon = 'el-icon-help'
      break
    }
  }
  return menu
}

function handleMenusList(menuList: IMenu[]): any[] {
  return menuList.reduce<IMenu[]>((acc, cur) => {
    // 去除读写权限等非页面菜单
    if (cur.type === 2 || cur.status === 0) {
      return acc
    }
    const obj: IMenu = {
      keyId: cur.keyId,
      name: cur.name,
      path: cur.path,
      status: cur.status,
      type: cur.type,
    }
    setMenuIcon(obj)
    if (Array.isArray(cur.children) && cur.children.length) {
      obj.children = handleMenusList(cur.children)
    }
    acc.push(obj)
    return acc
  }, [])
}

interface ILoginInfo {
  customerId: string
  customerType: number | string
  iconUrl: Record<string, any>[]
  accountId: string
  accountName: string
  orgId: string
  orgName: string
  token: string
}

interface IUserState {
  token: string
  userInfo: Record<string, any> // 用户中心数据
  loginInfo: ILoginInfo // 登录信息
  menus: unknown[]
  allMenus: unknown[]
  showMoreApp: boolean
}

export const state: () => IUserState = () => {
  const token = ''

  return {
    token,
    userInfo: {},
    loginInfo: {
      customerId: '',
      accountId: '',
      accountName: '',
      customerType: '',
      iconUrl: [],
      orgId: '',
      orgName: '',
      token: '',
    },
    menus: [],
    allMenus: [],
    showMoreApp: false,
  }
}

export const mutations: MutationTree<IUserState> = {
  [Mutations.SET_TOKEN](state, payload) {
    state.token = payload
    if (process.client) {
      setToken(payload)
    }
  },
  [Mutations.SET_BASEINFO](state, payload) {
    state.menus = payload.menus
  },
  [Mutations.SET_ALL_MENUES](state, payload) {
    state.allMenus = payload
  },
  [Mutations.SET_USER_INFO](state, payload) {
    state.userInfo = payload
  },
  [Mutations.SET_LOGIN_INFO](state, payload) {
    state.loginInfo = payload
  },
  [Mutations.SET_SHOW_MORE_APP](state, payload) {
    state.showMoreApp = payload
  },
}

export const actions: ActionTree<IUserState, IRootState> = {
  [Actions.setToken]({ commit }, token: string) {
    commit(Mutations.SET_TOKEN, token)
  },
  async [Actions.login]({ commit }, params: ILoginParams) {
    const result: any = await login(params)
    // const result2: any = await getCurrentUser()
    console.log('login', result)
    commit(Mutations.SET_TOKEN, result.token)
    commit(Mutations.SET_LOGIN_INFO, result)
    commit(Mutations.SET_USER_INFO, result)
    return result
  },
  async [Actions.phoneLogin]({ commit }, params: IPhoneLoginParams) {
    const result: any = await phoneLogin(params)
    // const result2: any = await getCurrentUser()
    console.log('phoneLogin', result)
    commit(Mutations.SET_TOKEN, result.token)
    commit(Mutations.SET_LOGIN_INFO, result)
    commit(Mutations.SET_USER_INFO, result)
    return result
  },
  async [Actions.logout]({ commit }) {
    // console.log($nuxt.$route)
    // const isAdmin = /^\/admin/.test($nuxt.$route.path)
    // if (isAdmin) {
    const result = await logout()
    commit(Mutations.SET_TOKEN, '')

    const isAdmin = /^\/admin/.test($nuxt.$route.path)
    // console.log(isAdmin)
    if (isAdmin) {
      this.$router.push('/login')
    }

    return result
    // }11111
    // return res
  },
  async [Actions.setBaseInfo]({ commit }) {
    const result: any = await findCurrentUserMenu()
    const menus = handleMenusList(result.menuList)
    console.log('%c [ menus ]-335', 'font-size:13px; background:pink; color:#bf2c9f;', menus)

    commit(Mutations.SET_ALL_MENUES, result.menuList)
    commit(Mutations.SET_BASEINFO, {
      menus, // innerMenus, // innerMenus, // result.menuList, // innerMenus, // data.result.menuList,
    })

    return menus // innerMenus // result.menuList // innerMenus
  },
  async [Actions.getUserInfo]({ commit }) {
    const result = await getCurrentUser()
    console.log(result)
    commit(Mutations.SET_USER_INFO, result)
    return result
  },
}

export const getters: GetterTree<IUserState, IRootState> = {
  orgId: (state) => state.loginInfo.orgId || state.userInfo.orgId,
  customerType: (state) => state.loginInfo.customerType || state.userInfo.customerType,
  customerId: (state) => state.loginInfo.customerId || state.userInfo.customerId,
  accountId: (state) => state.loginInfo.accountId || state.userInfo.id,
  token: (state) => state.token,
  authenticated: (state) => Boolean(state.token),
  menus: (state) => state.menus,
  allMenus: (state) => state.allMenus,
  userInfo: (state) => state.userInfo,
  showMoreApp: (state) => state.showMoreApp,
}
